package com.yixin.hospital.controller;

import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.common.Constant;
import com.yixin.hospital.entity.dto.req.jobType.JobTypeAddReqDTO;
import com.yixin.hospital.entity.dto.req.jobType.JobTypeEditReqDTO;
import com.yixin.hospital.service.JobTypeService;
import com.yixin.hospital.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@RequestMapping("/job-type")
@Api(value = "工作类型管理", tags = "工作类型管理")
@Slf4j
public class JobTypeController {

    @Resource
    private JobTypeService jobTypeService;

    @GetMapping("/list")
    @ApiOperation(value = "工作类型列表")
    public ApiResult list() {
        return ApiResult.success(jobTypeService.list());
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加工作类型")
    public ApiResult add(@Valid @RequestBody JobTypeAddReqDTO jobTypeAddReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return jobTypeService.add(jobTypeAddReqDTO, userName);
    }

    @PutMapping("/edit")
    @ApiOperation(value = "编辑工作类型")
    public ApiResult edit(@Valid @RequestBody JobTypeEditReqDTO jobTypeEditReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return jobTypeService.edit(jobTypeEditReqDTO, userName);
    }

    @DeleteMapping("/remove/{id}")
    @ApiOperation(value = "删除工作类型")
    public ApiResult remove(@PathVariable int id) {
        jobTypeService.removeById(id);
        return ApiResult.success();
    }
}
